//
//  SingleViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/19.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SingleViewController.h"
#import "AFNetworking.h"
#import "SingleVCModel.h"
#import "SingleTableViewCell.h"
#import "SingleCollectionViewCell.h"
#import "SingleSecondVCModel.h"
#import "SingleSecondDetailsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SingleViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)BaseCollectionView *myCollectionV;

@property(nonatomic,strong)NSMutableArray *myTableArray;
@property(nonatomic,strong)NSMutableArray *myCollectionArray;
@property(nonatomic,strong)NSMutableArray *myNewArray;




@end

@implementation SingleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  // self.view.backgroundColor = [UIColor whiteColor];

    self.myNewArray = [NSMutableArray array];
    self.myTableArray = [NSMutableArray array];
    [self getData];

    
    //tableview
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 80, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.myTableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[SingleTableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    
    
    //collectionview
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((WIDTH - 80 - 30) / 3, 100);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    self.myCollectionV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(80, 0, WIDTH - 80 , HEIGHT - 64 - 49 - 50) collectionViewLayout:flowLayout];
   // self.myCollectionV.pagingEnabled = YES;
    self.myCollectionV.showsHorizontalScrollIndicator = NO;
    self.myCollectionV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myCollectionV];
    
    self.myCollectionV.delegate = self;
    self.myCollectionV.dataSource = self;
    
    [self.myCollectionV registerClass:[SingleCollectionViewCell class] forCellWithReuseIdentifier:@"Creuse"];
    

    
    
    // Do any additional setup after loading the view.
}

-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    NSDictionary *dic = @{@"app_id":@"com.jzyd.BanTang",@"app_installtime":@"1434705307",@"app_versions":@"5.7",@"channel_name":@"appStore",@"client_id":@"bt_app_ios",@"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",@"is_new":@"1",@"os_versions":@"9.3.1",@"screensize":@"1242",@"track_device_info":@"iPhone7%2C1",@"track_deviceid":@"C2D3B460-4E0E-4639-A030-D274CFC43D50",@"v":@"12"};

    [manager POST:@"http://open3.bantangapp.com/category/list" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {


    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

       // NSLog(@"%@",responseObject);
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *dataArray = [bigDic objectForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            SingleVCModel *model = [[SingleVCModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.myTableArray addObject:model];
            
        }
//        NSDictionary *firstDic = [self.myTableArray objectAtIndex:0];
//        NSMutableArray *firstArray = [firstDic objectForKey:@"subclass"];
//        self.myNewArray = firstArray;
        
////            NSMutableArray *smallArray = [dic objectForKey:@"subclass"];
////            for (NSDictionary *dic in smallArray) {
////                SingleSecondVCModel *model = [[SingleSecondVCModel alloc] init];
////                [model setValuesForKeysWithDictionary:dic];
////                [self.myCollectionArray addObject:model];
            
//            }
        
        
      
       // [self.myCollectionV reloadData];
        
        [self.myTableView reloadData];
        [self.myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        SingleVCModel *model = [self.myTableArray objectAtIndex:0];
        self.myNewArray = [NSMutableArray arrayWithArray:model.subclass];
        
        [self.myCollectionV reloadData];


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


    }];



}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    NSMutableDictionary *bigdic =[self.myTableArray objectAtIndex:indexPath.row];
//    
//    self.myCollectionArray = [bigdic objectForKey:@"subclass"];
//    
   // NSMutableArray *classArray = [bigdic objectForKey:@"subclass"];
//    for (NSDictionary *dic in self.myCollectionArray) {
//        SingleSecondVCModel *model = [[SingleSecondVCModel alloc] init];
//        [model setValuesForKeysWithDictionary:dic];
//        [self.myCollectionArray addObject:model];
//    }
    
    SingleVCModel *model = [self.myTableArray objectAtIndex:indexPath.row];
    self.myNewArray = [NSMutableArray arrayWithArray:model.subclass];

    [self.myCollectionV reloadData];
    


}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    SingleCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"Creuse" forIndexPath:indexPath];
    
    
    item.SinSModel = [self.myNewArray objectAtIndex:indexPath.row];
  //  item.backgroundColor = [UIColor yellowColor];
  //  item.backgroundColor = [UIColor purpleColor];
    return  item;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.myNewArray.count;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.myTableArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    SingleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.sinModel = [self.myTableArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;



}

//点击collectionviewcell 跳转 details
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    SingleSecondDetailsViewController *SSDVC = [[SingleSecondDetailsViewController alloc] init];
    SSDVC.sinSVCModel = [self.myNewArray objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:SSDVC animated:YES];



}

@end
